/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.guit.demo.client.complexplace;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.inject.Inject;

import com.guit.client.GuitPresenter;
import com.guit.client.View;
import com.guit.client.apt.GwtPresenter;
import com.guit.client.binder.GuitBinder;
import com.guit.client.binder.ViewField;
import com.guit.client.binder.ViewHandler;
import com.guit.client.binder.contributor.RunAsync;
import com.guit.client.binder.contributor.UserAction;
import com.guit.client.place.StayPlace;
import com.guit.demo.client.complexplace.ComplexPlacePresenter.ComplexPlaceBinder;
import com.guit.demo.client.main.MainPresenter;
import com.guit.demo.client.styler.Styler;

@RunAsync
@GwtPresenter(placeName = "complex")
public class ComplexPlacePresenter extends GuitPresenter<ComplexPlaceBinder> implements
    StayPlace<ComplexPlaceData> {

  public interface ComplexPlaceBinder extends GuitBinder<ComplexPlacePresenter> {
  }

  private final MainPresenter hasContent;
  ComplexPlaceData data;

  @ViewField
  HasText textbox;

  @ViewField
  HasValue<Boolean> checkbox;

  @ViewField
  Styler color;

  @ViewField
  Styler fontSize;

  @Inject
  public ComplexPlacePresenter(MainPresenter hasContent) {
    this.hasContent = hasContent;
  }

  @ViewHandler(event = ValueChangeEvent.class, fields = {"checkbox", "textbox"})
  @UserAction("Checkbox or textbox change")
  void checkboxOrTextboxChange(Object source) {
    if (source.equals(checkbox)) {
      data.setEnabled(checkbox.getValue());
    } else if (source.equals(textbox)) {
      data.setText(textbox.getText());
    } else {
      throw new IllegalStateException();
    }
    registerChange();
  }

  @ViewHandler
  void color$click() {
    if (color.getBackgroundColor().equals("red")) {
      data.setColor("blue");
    } else {
      data.setColor("red");
    }
    registerChange();
  }

  @Override
  public void go(ComplexPlaceData data) {
    if (data == null) {
      data = new ComplexPlaceData();
      data.setColor("red");
      data.setEnabled(true);
      data.setText("Hello");
      data.setTextSize(23);
      placeManager.newItem(ComplexPlacePresenter.class, data);
    }

    View view = getView();
    if (data.getColor().equals("blue")) {
      hasContent.setContent(view, 2);
    } else {
      hasContent.setContent(view, 3);
    }

    this.data = data;
    render();
  }

  @Override
  public void leave() {
    // alerter.alert("Leaving ComplexPlace..."); // We can show an alert,
    // but its annoyng
  }

  @Override
  public String mayLeave() {
    return "Sure you wanna leave?";
  }

  private void registerChange() {
    placeManager.newItem(ComplexPlacePresenter.class, data);
    render();
  }

  private void render() {
    textbox.setText(data.getText());
    checkbox.setValue(data.isEnabled());
    color.setBackgroundColor(data.getColor());
    fontSize.setFontSizePx(data.getTextSize());
  }

  @Override
  public void stay() {
    // alerter.alert("Staying in ComplexPlace..."); // We can show an alert,
    // but its annoyng
  }
}
